package tech.lq0.dreamaticvoyage.datagen;

import net.minecraft.advancements.Advancement;
import net.minecraft.advancements.RequirementsStrategy;
import net.minecraft.advancements.critereon.EntityPredicate;
import net.minecraft.advancements.critereon.MinMaxBounds;
import net.minecraft.advancements.critereon.MobEffectsPredicate;
import net.minecraft.advancements.critereon.TargetBlockTrigger;
import net.minecraft.data.CachedOutput;
import net.minecraft.data.DataProvider;
import net.minecraft.data.PackOutput;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackType;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.data.ExistingFileHelper;
import tech.lq0.dreamaticvoyage.advancements.criterion.CallInThunderTrigger;
import tech.lq0.dreamaticvoyage.advancements.criterion.FillLavaTrigger;
import tech.lq0.dreamaticvoyage.init.EffectRegistry;
import tech.lq0.dreamaticvoyage.init.EntityRegistry;
import tech.lq0.dreamaticvoyage.init.ItemRegistry;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.UnaryOperator;

/**
 * Codes Based on @Create
 */
@SuppressWarnings("unused")
public class ModAdvancementProvider implements DataProvider {

    private final PackOutput packOutput;
    private final ExistingFileHelper existingFileHelper;

    public static final List<ModAdvancement> ADVANCEMENTS = new ArrayList<>();

    public static ModAdvancement START = null,
    /**
     * Main
     */
    MAIN_ROOT = advancement("root", builder -> builder.icon(ItemRegistry.RED_AHOGE.get()).awardedForFree()),

    // 1st
    RED_AHOGE_HELMET = advancement("red_ahoge_helmet", builder -> builder.icon(ItemRegistry.RED_AHOGE_HELMET.get()).whenIconCollected()
            .parent(MAIN_ROOT)),

    RED_AHOGE_WEAPON_MASTER = advancement("red_ahoge_weapon_master", builder -> builder.icon(ItemRegistry.RED_AHOGE_BOOMERANG.get()).whenIconCollected()
            .whenItemConsumed(ItemRegistry.RED_AHOGE_SWORD.get()).whenItemCollected(ItemRegistry.RED_AHOGE_MACHETE.get()).whenItemCollected(ItemRegistry.CRIMSON_IMPACT.get())
            .type(ModAdvancement.Type.GOAL)
            .parent(RED_AHOGE_HELMET)),

    DRINK_PEACH_BEACH = advancement("drink_peach_beach", builder -> builder.icon(ItemRegistry.PEACH_BEACH.get()).whenIconConsumed()
            .parent(RED_AHOGE_HELMET)),

    SCHEDULE = advancement("schedule", builder -> builder.icon(ItemRegistry.SCHEDULE.get()).whenIconCollected()
            .parent(RED_AHOGE_HELMET)),

    MOUNTAIN_DESTROYER = advancement("mountain_destroyer", builder -> builder.icon(ItemRegistry.MOUNTAIN_DESTROYER.get()).whenIconCollected()
            .type(ModAdvancement.Type.DEFAULT_CHALLENGE)
            .parent(SCHEDULE)),

    DRINK_BANANA_MILK = advancement("drink_banana_milk", builder -> builder.icon(ItemRegistry.BANANA_MILK.get()).whenIconConsumed()
            .parent(SCHEDULE)),

    EAT_TARO_ICE_CREAM = advancement("eat_taro_ice_cream", builder -> builder.icon(ItemRegistry.TARO_ICE_CREAM.get()).whenIconConsumed()
            .parent(DRINK_BANANA_MILK)),

    EAT_UNIGIRI = advancement("eat_unigiri", builder -> builder.icon(ItemRegistry.UNIGIRI.get()).whenIconConsumed()
            .parent(EAT_TARO_ICE_CREAM)),

    EAT_ENCHANTED_UNIGIRI = advancement("eat_enchanted_unigiri", builder -> builder.icon(ItemRegistry.ENCHANTED_UNIGIRI.get()).whenIconConsumed()
            .type(ModAdvancement.Type.GOAL)
            .parent(EAT_UNIGIRI)),

    CRYSTAL_HORN = advancement("crystal_horn", builder -> builder.icon(ItemRegistry.CRYSTAL_HORN.get()).whenIconCollected()
            .parent(EAT_TARO_ICE_CREAM)),

    SLEEP_ROULETTE = advancement("sleep_roulette", builder -> builder.icon(ItemRegistry.SLEEP_ROULETTE.get()).whenIconCollected()
            .parent(CRYSTAL_HORN)),

    EAT_MARI_STEW = advancement("eat_mari_stew", builder -> builder.icon(ItemRegistry.MARI_STEW.get()).whenIconConsumed()
            .parent(CRYSTAL_HORN)),

    MACHETE = advancement("machete", builder -> builder.icon(ItemRegistry.MACHETE.get()).whenIconCollected()
            .parent(EAT_MARI_STEW)),

    MAGIC_MIRROR = advancement("magic_mirror", builder -> builder.icon(ItemRegistry.MAGIC_MIRROR.get()).whenIconCollected()
            .type(ModAdvancement.Type.GOAL)
            .parent(EAT_MARI_STEW)),

    BIG_SPOON = advancement("big_spoon", builder -> builder.icon(ItemRegistry.BIG_SPOON.get()).whenIconCollected()
            .parent(EAT_MARI_STEW)),

    HAYAMEN = advancement("hayamen", builder -> builder.icon(ItemRegistry.HAYAMEN.get()).whenIconCollected()
            .type(ModAdvancement.Type.DEFAULT_CHALLENGE)
            .parent(BIG_SPOON)),

    // 2nd
    EAT_MUMU_STEAK = advancement("eat_mumu_steak", builder -> builder.icon(ItemRegistry.MUMU_STEAK.get()).whenIconConsumed()
            .parent(MAIN_ROOT)),

    AYELET_MASK = advancement("ayelet_mask", builder -> builder.icon(ItemRegistry.AYELET_MASK.get()).whenIconCollected()
            .parent(EAT_MUMU_STEAK)),

    ELECTRIC_HORN = advancement("electric_horn", builder -> builder.icon(ItemRegistry.ELECTRIC_HORN.get()).whenIconCollected()
            .parent(EAT_MUMU_STEAK)),

    DUEL_WATER_GUN = advancement("duel_water_gun", builder -> builder.icon(ItemRegistry.DUEL_WATER_GUN.get())
            .type(ModAdvancement.Type.SECRET_CHALLENGE)
            .externalTrigger(FillLavaTrigger.TriggerInstance.item(ItemRegistry.DUEL_WATER_GUN.get()))
            .parent(ELECTRIC_HORN)),

    SATOU_KNIFE = advancement("satou_knife", builder -> builder.icon(ItemRegistry.SATOU_KNIFE.get()).whenIconCollected()
            .parent(ELECTRIC_HORN)),

    SATOU_CAMERA = advancement("satou_camera", builder -> builder.icon(ItemRegistry.SATOU_CAMERA.get()).whenIconCollected()
            .parent(SATOU_KNIFE)),

    EAT_LUCIA_RICE_CAKE = advancement("eat_lucia_rice_cake", builder -> builder.icon(ItemRegistry.LUCIA_RICE_CAKE.get()).whenIconConsumed()
            .parent(SATOU_KNIFE)),

    BIG_MOE_ONE = advancement("big_moe_one", builder -> builder.icon(ItemRegistry.BIG_MOE_ONE_POTION.get())
            .whenEffectChanged(MobEffectsPredicate.effects().and(EffectRegistry.BIG_MOE_ONE.get()))
            .type(ModAdvancement.Type.GOAL)
            .parent(EAT_LUCIA_RICE_CAKE)),

    MOONLIGHT_AMULET = advancement("moonlight_amulet", builder -> builder.icon(ItemRegistry.MOONLIGHT_AMULET.get()).whenIconCollected()
            .parent(BIG_MOE_ONE)),

    WHITE_DOUBLE_AHOGE = advancement("white_double_ahoge", builder -> builder.icon(ItemRegistry.WHITE_DOUBLE_AHOGE.get()).whenIconCollected()
            .parent(EAT_LUCIA_RICE_CAKE)),

    MAGIC_BOOK = advancement("magic_book", builder -> builder.icon(ItemRegistry.MAGIC_BOOK.get()).whenIconCollected()
            .parent(WHITE_DOUBLE_AHOGE)),

    MAGIC_BOOK_MYANNA = advancement("magic_book_myanna", builder -> builder.icon(ItemRegistry.MAGIC_BOOK_MYANNA.get()).whenIconCollected()
            .parent(MAGIC_BOOK)),

    MAGIC_BOOK_MYANNA_PLUS = advancement("magic_book_myanna_plus", builder -> builder.icon(ItemRegistry.MAGIC_BOOK_MYANNA_PLUS.get()).whenIconCollected()
            .type(ModAdvancement.Type.GOAL)
            .parent(MAGIC_BOOK_MYANNA)),

    // Magicros
    FROG_CROWN = advancement("frog_crown", builder -> builder.icon(ItemRegistry.FROG_CROWN.get()).whenIconCollected()
            .parent(MAIN_ROOT)),

    EAT_FROG_LEG = advancement("eat_frog_leg", builder -> builder.icon(ItemRegistry.FROG_LEG.get()).whenIconConsumed()
            .parent(FROG_CROWN)),

    EAT_MARSHAIMALLOW = advancement("eat_marshaimallow", builder -> builder.icon(ItemRegistry.MARSHAIMALLOW.get()).whenIconConsumed()
            .parent(FROG_CROWN)),

    CLOUD_KEY = advancement("cloud_key", builder -> builder.icon(ItemRegistry.CLOUD_KEY.get()).whenIconCollected()
            .type(ModAdvancement.Type.GOAL)
            .parent(EAT_MARSHAIMALLOW)),

    CURSED_MARSHAIMALLOW = advancement("cursed_marshaimallow", builder -> builder.icon(ItemRegistry.CURSED_MARSHAIMALLOW.get()).whenIconCollected()
            .parent(EAT_MARSHAIMALLOW)),

    SUCCUBUS_TAIL = advancement("succubus_tail", builder -> builder.icon(ItemRegistry.SUCCUBUS_TAIL.get()).whenIconCollected()
            .parent(EAT_MARSHAIMALLOW)),

    DRINK_BUBBLE_TEA = advancement("drink_bubble_tea", builder -> builder.icon(ItemRegistry.BUBBLE_TEA.get()).whenIconConsumed()
            .parent(SUCCUBUS_TAIL)),

    EAT_LOTUS_POTATO = advancement("eat_lotus_potato", builder -> builder.icon(ItemRegistry.LOTUS_POTATO.get()).whenIconConsumed()
            .parent(SUCCUBUS_TAIL)),

    FLAT_VEGETABLE_CHESTPLATE = advancement("flat_vegetable_chestplate", builder -> builder.icon(ItemRegistry.FLAT_VEGETABLE_CHESTPLATE.get()).whenIconCollected()
            .parent(EAT_LOTUS_POTATO)),

    STEEL_PLATE = advancement("steel_plate", builder -> builder.icon(ItemRegistry.STEEL_PLATE.get()).whenIconCollected()
            .parent(FLAT_VEGETABLE_CHESTPLATE)),

    PARALLEL_PLATE_CAPACITOR = advancement("parallel_plate_capacitor", builder -> builder.icon(ItemRegistry.HAI_PLATE.get()).whenIconCollected()
            .whenItemCollected(ItemRegistry.FLAT_VEGETABLE_CHESTPLATE.get())
            .type(ModAdvancement.Type.GOAL)
            .parent(FLAT_VEGETABLE_CHESTPLATE)),

    KERORO_IDOL_COSTUME = advancement("keroro_idol_costume", builder -> builder.icon(ItemRegistry.KERORO_IDOL_CHESTPLATE.get()).whenIconCollected()
            .whenItemCollected(ItemRegistry.KERORO_IDOL_HELMET.get()).whenItemConsumed(ItemRegistry.KERORO_IDOL_BOOTS.get())
            .parent(FROG_CROWN)),

    HAINE_IDOL_COSTUME = advancement("haine_idol_costume", builder -> builder.icon(ItemRegistry.HAINE_IDOL_CHESTPLATE.get()).whenIconCollected()
            .whenItemCollected(ItemRegistry.HAINE_IDOL_HELMET.get()).whenItemConsumed(ItemRegistry.HAINE_IDOL_BOOTS.get())
            .parent(KERORO_IDOL_COSTUME)),

    EKIRA_IDOL_COSTUME = advancement("ekira_idol_costume", builder -> builder.icon(ItemRegistry.EKIRA_IDOL_CHESTPLATE.get()).whenIconCollected()
            .whenItemCollected(ItemRegistry.EKIRA_IDOL_HELMET.get()).whenItemConsumed(ItemRegistry.EKIRA_IDOL_BOOTS.get())
            .parent(HAINE_IDOL_COSTUME)),

    CHIRAM_IDOL_COSTUME = advancement("chiram_idol_costume", builder -> builder.icon(ItemRegistry.CHIRAM_IDOL_CHESTPLATE.get()).whenIconCollected()
            .whenItemCollected(ItemRegistry.CHIRAM_IDOL_HELMET.get()).whenItemConsumed(ItemRegistry.CHIRAM_IDOL_BOOTS.get())
            .parent(EKIRA_IDOL_COSTUME)),

    DREAM_WEAVING_YARN = advancement("dream_weaving_yarn", builder -> builder.icon(ItemRegistry.DREAM_WEAVING_YARN.get()).whenIconCollected()
            .type(ModAdvancement.Type.DEFAULT_CHALLENGE)
            .rewardExp(100)
            .parent(CHIRAM_IDOL_COSTUME)),

    MAGICROS_CORE = advancement("magicros_core", builder -> builder.icon(ItemRegistry.MAGICROS_CORE.get())
            .whenItemCollected(ItemRegistry.CHIRAM_CORE.get()).whenItemCollected(ItemRegistry.EKIRA_CORE.get())
            .whenItemCollected(ItemRegistry.HAINE_CORE.get()).whenItemCollected(ItemRegistry.KERORO_CORE.get())
            .parent(DREAM_WEAVING_YARN)),

    // 3rd
    EAT_FOX_ICE_CREAM = advancement("eat_fox_ice_cream", builder -> builder.icon(ItemRegistry.FOX_ICE_CREAM.get()).whenIconConsumed()
            .parent(MAIN_ROOT)),

    HIRU_HEAD = advancement("hiru_head", builder -> builder.icon(ItemRegistry.HIRU_HEAD.get()).whenIconCollected()
            .parent(EAT_FOX_ICE_CREAM)),

    REINA_WINGS = advancement("reina_wings", builder -> builder.icon(ItemRegistry.REINA_WINGS.get()).whenIconCollected()
            .parent(HIRU_HEAD)),

    // 4th
    NETHER_LIGHT = advancement("nether_light", builder -> builder.icon(ItemRegistry.NETHER_LIGHT.get()).whenIconCollected()
            .parent(MAIN_ROOT)),

    EAT_STRAWBERRY_CAKE = advancement("eat_strawberry_cake", builder -> builder.icon(ItemRegistry.STRAWBERRY_CAKE.get()).whenIconConsumed()
            .parent(NETHER_LIGHT)),

    JELLYFISH_BOTTLE = advancement("jellyfish_bottle", builder -> builder.icon(ItemRegistry.JELLYFISH_BOTTLE.get()).whenIconCollected()
            .parent(EAT_STRAWBERRY_CAKE)),

    // 0th
    MITSUKI_CROWN = advancement("mitsuki_crown", builder -> builder.icon(ItemRegistry.MITSUKI_CROWN.get()).whenIconCollected()
            .parent(MAIN_ROOT)),

    PLUNGER = advancement("plunger", builder -> builder.icon(ItemRegistry.PLUNGER.get())
            .externalTrigger(TargetBlockTrigger.TriggerInstance.targetHit(MinMaxBounds.Ints.exactly(15), EntityPredicate.wrap(EntityPredicate.Builder.entity().of(EntityRegistry.PLUNGER_ENTITY.get()).build())))
            .type(ModAdvancement.Type.GOAL)
            .parent(MITSUKI_CROWN)),

    KRM_963_53 = advancement("krm_963_53", builder -> builder.icon(ItemRegistry.KRM_963_53.get()).whenIconCollected()
            .type(ModAdvancement.Type.DEFAULT_CHALLENGE)
            .parent(PLUNGER)),

    // OI
    TAIL_PEN = advancement("tail_pen", builder -> builder.icon(ItemRegistry.TAIL_PEN.get()).whenIconCollected()
            .parent(MAIN_ROOT)),

    FOX_FAIRY_FAN = advancement("fox_fairy_fan", builder -> builder.icon(ItemRegistry.FOX_FAIRY_FAN.get()).whenIconCollected()
            .parent(TAIL_PEN)),

    GRAVITY_RESTRAINT_DEVICE = advancement("gravity_restraint_device", builder -> builder.icon(ItemRegistry.GRAVITY_RESTRAINT_DEVICE.get()).whenIconCollected()
            .parent(FOX_FAIRY_FAN)),

    EAT_RABBIT_LYCHEE = advancement("eat_rabbit_lychee", builder -> builder.icon(ItemRegistry.RABBIT_LYCHEE.get()).whenIconConsumed()
            .parent(GRAVITY_RESTRAINT_DEVICE)),

    FUKAMIZU_RING = advancement("fukamizu_ring", builder -> builder.icon(ItemRegistry.FUKAMIZU_RING.get()).whenIconCollected()
            .type(ModAdvancement.Type.DEFAULT_CHALLENGE)
            .parent(EAT_RABBIT_LYCHEE)),

    FIAXE = advancement("fiaxe", builder -> builder.icon(ItemRegistry.FIAXE.get()).whenIconCollected()
            .parent(FUKAMIZU_RING)),

    EAT_MINT_CHOCOLATE = advancement("eat_mint_chocolate", builder -> builder.icon(ItemRegistry.MINT_CHOCOLATE.get()).whenIconConsumed()
            .parent(FIAXE)),

    EAT_XIAOLONGBAO = advancement("eat_xiaolongbao", builder -> builder.icon(ItemRegistry.XIAOLONGBAO.get()).whenIconConsumed()
            .parent(EAT_MINT_CHOCOLATE)),

    SHU_RINO_KEN = advancement("shu_rino_ken", builder -> builder.icon(ItemRegistry.SHU_RINO_KEN.get()).whenIconCollected()
            .parent(EAT_XIAOLONGBAO)),

    DRINK_CRAFT_BEER_0322 = advancement("drink_craft_beer_0322", builder -> builder.icon(ItemRegistry.CRAFT_BEER_0322.get()).whenIconConsumed()
            .parent(SHU_RINO_KEN)),

    QIANYUN_HOE = advancement("qianyun_hoe", builder -> builder.icon(ItemRegistry.QIANYUN_HOE.get()).whenIconCollected()
            .parent(MAIN_ROOT)),

    // Shirako
    EAT_MOMO_DAIFUKU = advancement("eat_momo_daifuku", builder -> builder.icon(ItemRegistry.MOMO_DAIFUKU.get()).whenIconConsumed()
            .parent(MAIN_ROOT)),

    FETUOZI = advancement("fetuozi", builder -> builder.icon(ItemRegistry.FETUOZI.get()).whenIconCollected()
            .parent(EAT_MOMO_DAIFUKU)),

    MOMO_KNIFE = advancement("momo_knife", builder -> builder.icon(ItemRegistry.MOMO_KNIFE.get()).whenIconCollected()
            .type(ModAdvancement.Type.GOAL)
            .parent(FETUOZI)),

    MOMO_PHONE = advancement("momo_phone", builder -> builder.icon(ItemStack.of(makeMomoPhoneTags()))
            .whenItemCollected(ItemRegistry.MOMO_PHONE.get())
            .parent(EAT_MOMO_DAIFUKU)),

    CALL_IN_THUNDER = advancement("call_in_thunder", builder -> builder.icon(ItemRegistry.MOMO_PHONE.get())
            .type(ModAdvancement.Type.SECRET_CHALLENGE)
            .externalTrigger(CallInThunderTrigger.TriggerInstance.item(ItemRegistry.MOMO_PHONE.get()))
            .parent(MOMO_PHONE)),

    // Special
    LEAVE_PROVIDENCE = advancement("leave_providence", builder -> builder.icon(ItemRegistry.FLUFF_BALL.get()).whenIconCollected()
            .type(ModAdvancement.Type.SECRET_CHALLENGE)
            .parent(MAIN_ROOT)),

    POINTS_STORE = advancement("points_store", builder -> builder.icon(ItemRegistry.POINTS_STORE.get()).whenIconCollected()
            .parent(MAIN_ROOT)),

    PREMIUM_PDC_POINT = advancement("premium_pdc_point", builder -> builder.icon(ItemRegistry.PREMIUM_PDC_POINT.get()).whenIconCollected()
            .type(ModAdvancement.Type.DEFAULT_CHALLENGE)
            .parent(POINTS_STORE)),

    APEX_GOURMET = advancement("apex_gourmet", builder -> builder.icon(ItemRegistry.MMA_STEW.get())
            .type(ModAdvancement.Type.DEFAULT_CHALLENGE)
            .whenItemConsumed(ItemRegistry.PEACH_BEACH.get())
            .whenItemConsumed(ItemRegistry.BANANA_MILK.get())
            .whenItemConsumed(ItemRegistry.TARO_ICE_CREAM.get())
            .whenItemConsumed(ItemRegistry.UNIGIRI.get())
            .whenItemConsumed(ItemRegistry.ENCHANTED_UNIGIRI.get())
            .whenItemConsumed(ItemRegistry.MARI_STEW.get())
            .whenItemConsumed(ItemRegistry.BROWNIE_UZZA.get())
            .whenItemConsumed(ItemRegistry.MUMU_STEAK.get())
            .whenItemConsumed(ItemRegistry.MUSHROOM_CROISSANT.get())
            .whenItemConsumed(ItemRegistry.KULEBOH.get())
            .whenItemConsumed(ItemRegistry.ISEKAI_LOLLIPOP.get())
            .whenItemConsumed(ItemRegistry.LUCIA_RICE_CAKE.get())
            .whenItemConsumed(ItemRegistry.BIG_FIERCE_ONE_COFFEE.get())
            .whenItemConsumed(ItemRegistry.LOUISTEW.get())
            .whenItemConsumed(ItemRegistry.UNI_MILLET.get())
            .whenItemConsumed(ItemRegistry.LOTUS_POTATO.get())
            .whenItemConsumed(ItemRegistry.BOWL_OF_VEGETABLE.get())
            .whenItemConsumed(ItemRegistry.MARSHAIMALLOW.get())
            .whenItemConsumed(ItemRegistry.HAIBARA_SOUP.get())
            .whenItemConsumed(ItemRegistry.FROG_LEG.get())
            .whenItemConsumed(ItemRegistry.LEMON_TEA.get())
            .whenItemConsumed(ItemRegistry.LARGE_LEMON_TEA.get())
            .whenItemConsumed(ItemRegistry.BUBBLE_TEA.get())
            .whenItemConsumed(ItemRegistry.HIRENADE_GG.get())
            .whenItemConsumed(ItemRegistry.FOX_ICE_CREAM.get())
            .whenItemConsumed(ItemRegistry.FOX_PUDDING.get())
            .whenItemConsumed(ItemRegistry.MOMO_DAIFUKU.get())
            .whenItemConsumed(ItemRegistry.DAIFUKU_SYRUP.get())
            .whenItemConsumed(ItemRegistry.CHOCOLATE_BOOTS.get())
            .whenItemConsumed(ItemRegistry.RABBIT_LYCHEE.get())
            .whenItemConsumed(ItemRegistry.FUKAMIZU_BREAD.get())
            .whenItemConsumed(ItemRegistry.FUKAMIZU_FISH.get())
            .whenItemConsumed(ItemRegistry.GARLIC_MELON_SLICE.get())
            .whenItemConsumed(ItemRegistry.FUKAMIZU_PLUM.get())
            .whenItemConsumed(ItemRegistry.MINT_CHOCOLATE.get())
            .whenItemConsumed(ItemRegistry.XIAOLONGBAO.get())
            .whenItemConsumed(ItemRegistry.CRAFT_BEER_0322.get())
            .whenItemConsumed(ItemRegistry.STRAWBERRY_CAKE.get())
            .whenItemConsumed(ItemRegistry.CHOCOAL_COOKIE.get())
            .whenItemConsumed(ItemRegistry.STRAWBERRY_CHOCOLATE.get())
            .whenItemConsumed(ItemRegistry.SHAKANA.get())
            .whenItemConsumed(ItemRegistry.MMA_STEW.get())
            .whenItemConsumed(ItemRegistry.MVA_STEW.get())
            .whenItemConsumed(ItemRegistry.CRYSTAL_POPCORN.get())
            .rewardExp(1000)
            .parent(MAIN_ROOT)),

    /**
     * Legendary
     */
    LEGENDARY_ROOT = advancement("root", builder -> builder.icon(ItemRegistry.FRACTURE_DESTINY.get())
            .whenItemCollected(ItemRegistry.ANTI_INGOT.get())
            .whenItemCollected(ItemRegistry.GACHI_INGOT.get())
            .requirement(RequirementsStrategy.OR)
            .group(ModAdvancement.Group.LEGENDARY)),

    ANCIENT_LOLLIPOP = advancement("ancient_lollipop", builder -> builder.icon(ItemRegistry.ANCIENT_LOLLIPOP.get()).whenIconCollected()
            .type(ModAdvancement.Type.DEFAULT_CHALLENGE)
            .group(ModAdvancement.Group.LEGENDARY)
            .parent(LEGENDARY_ROOT)),

    WORLD_PEACE_STAFF = advancement("world_peace_staff", builder -> builder.icon(ItemRegistry.WORLD_PEACE_STAFF.get()).whenIconCollected()
            .type(ModAdvancement.Type.DEFAULT_CHALLENGE)
            .group(ModAdvancement.Group.LEGENDARY)
            .parent(LEGENDARY_ROOT)),

    ECHOED_DESTINY_RING = advancement("echoed_destiny_ring", builder -> builder.icon(ItemRegistry.ECHOED_DESTINY_RING.get()).whenIconCollected()
            .type(ModAdvancement.Type.DEFAULT_CHALLENGE)
            .group(ModAdvancement.Group.LEGENDARY)
            .parent(LEGENDARY_ROOT)),

    BREEZE_CROWN = advancement("breeze_crown", builder -> builder.icon(ItemRegistry.BREEZE_CROWN.get()).whenIconCollected()
            .type(ModAdvancement.Type.DEFAULT_CHALLENGE)
            .group(ModAdvancement.Group.LEGENDARY)
            .parent(LEGENDARY_ROOT)),

    MISTY_CHESTPLATE = advancement("misty_chestplate", builder -> builder.icon(ItemRegistry.MISTY_CHESTPLATE.get()).whenIconCollected()
            .type(ModAdvancement.Type.DEFAULT_CHALLENGE)
            .group(ModAdvancement.Group.LEGENDARY)
            .parent(LEGENDARY_ROOT)),

    FROGGY_LEGGINGS = advancement("froggy_leggings", builder -> builder.icon(ItemRegistry.FROGGY_LEGGINGS.get()).whenIconCollected()
            .type(ModAdvancement.Type.DEFAULT_CHALLENGE)
            .group(ModAdvancement.Group.LEGENDARY)
            .parent(LEGENDARY_ROOT)),

    CELESTIAL_BOOTS = advancement("celestial_boots", builder -> builder.icon(ItemRegistry.CELESTIAL_BOOTS.get()).whenIconCollected()
            .type(ModAdvancement.Type.DEFAULT_CHALLENGE)
            .group(ModAdvancement.Group.LEGENDARY)
            .parent(LEGENDARY_ROOT)),

    OMINOUS_SICKLE = advancement("ominous_sickle", builder -> builder.icon(ItemRegistry.OMINOUS_SICKLE.get()).whenIconCollected()
            .type(ModAdvancement.Type.DEFAULT_CHALLENGE)
            .group(ModAdvancement.Group.LEGENDARY)
            .parent(LEGENDARY_ROOT)),

    LEVIY = advancement("leviy", builder -> builder.icon(ItemRegistry.LEVIY.get()).whenIconCollected()
            .type(ModAdvancement.Type.DEFAULT_CHALLENGE)
            .group(ModAdvancement.Group.LEGENDARY)
            .parent(LEGENDARY_ROOT)),

    CHOCOLUSION = advancement("chocolusion", builder -> builder.icon(ItemRegistry.CHOCOLUSION.get()).whenIconCollected()
            .type(ModAdvancement.Type.DEFAULT_CHALLENGE)
            .group(ModAdvancement.Group.LEGENDARY)
            .parent(LEGENDARY_ROOT)),

    END = null;

    private static CompoundTag makeMomoPhoneTags() {
        ItemStack stack = ItemRegistry.MOMO_PHONE.get().getDefaultInstance();
        stack.getOrCreateTag().putBoolean("binding", true);
        return stack.serializeNBT();
    }

    public ModAdvancementProvider(PackOutput output, ExistingFileHelper existingFileHelper) {
        this.packOutput = output;
        this.existingFileHelper = existingFileHelper;
    }

    private static ModAdvancement advancement(String id, UnaryOperator<ModAdvancement.Builder> b) {
        return new ModAdvancement(id, b);
    }

    @Override
    public CompletableFuture<?> run(CachedOutput pOutput) {
        List<CompletableFuture<?>> futures = new ArrayList<>();
        PackOutput.PathProvider pathProvider = packOutput.createPathProvider(PackOutput.Target.DATA_PACK, "advancements");

        Consumer<Advancement> mainConsumer = (advancement) -> {
            ResourceLocation id = advancement.getId();
            if (existingFileHelper.exists(id, PackType.SERVER_DATA, ".json", "advancements")) {
                throw new IllegalStateException("Duplicate advancement " + id);
            }
            Path path = pathProvider.json(id);
            futures.add(DataProvider.saveStable(pOutput, advancement.deconstruct().serializeToJson(), path));
        };

        for (ModAdvancement advancement : ADVANCEMENTS) {
            advancement.save(mainConsumer);
        }

        return CompletableFuture.allOf(futures.toArray(CompletableFuture[]::new));
    }

    @Override
    public String getName() {
        return "Dreamatic Voyage Advancements";
    }
}
